package com.aaa.common.excel;
import com.aaa.common.domain.RentOutContractCURDInfo;
import com.aaa.common.result.Result;
import com.aaa.service.impl.RentOutContractAddServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 宋文森
 * @Date: 2021/02/23/16:55
 * @Description: 合同的导入和导出业务
 */
@RestController
@RequestMapping("/excel")
public class ImportAndExport {
    @Autowired
    private RentOutContractAddServiceImpl rentOutAddService;

    /**
     * excel表格信息导入数据库
     * @param stus
     * @return
     * @throws IOException
     */
    @RequestMapping("/import")
    public Result importExcel(MultipartFile stus) throws IOException {

      /*  XSSFWorkbook sheets1 = new XSSFWorkbook(stus.getInputStream());
        XSSFSheet sheet = sheets1.getSheetAt(0);*/
       HSSFWorkbook sheets = new HSSFWorkbook(   stus.getInputStream()  );
        HSSFSheet sheet = sheets.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();


        for (int i=1; i<=lastRowNum;i++){

            HSSFRow row = sheet.getRow(i);

            String contractId = row.getCell(0).getStringCellValue();
            String assetId = row.getCell(1).getStringCellValue();
            String renterType = row.getCell(2).getStringCellValue();
            String systemRent = row.getCell(3).getStringCellValue();
            String counterBooth = row.getCell(4).getStringCellValue();
            String rentUse = row.getCell(5).getStringCellValue();
            String subletState = row.getCell(6).getStringCellValue();
            Date contractBeginDate = row.getCell(7).getDateCellValue();
            Date contractEndDate = row.getCell(8).getDateCellValue();
            Date freeBeginDate = row.getCell(9).getDateCellValue();
            Date freeEndDate = row.getCell(10).getDateCellValue();
            String growthRate = row.getCell(11).getStringCellValue();
            String deposit = row.getCell(12).getStringCellValue();
            int contractStatusId = (int) row.getCell(13).getNumericCellValue();
            int businessStatusId = (int) row.getCell(14).getNumericCellValue();
            String remark = row.getCell(15).getStringCellValue();
            String listed = row.getCell(16).getStringCellValue();
            String lessor = row.getCell(17).getStringCellValue();
            String rentInContractId = row.getCell(18).getStringCellValue();
            int annualRent = (int) row.getCell(19).getNumericCellValue();
            int payMethodId = (int) row.getCell(20).getNumericCellValue();
            int rentchargeStatusId = (int) row.getCell(21).getNumericCellValue();
            String name = row.getCell(22).getStringCellValue();
            String contact = row.getCell(23).getStringCellValue();
            String phone = row.getCell(24).getStringCellValue();
            String companyName = row.getCell(25).getStringCellValue();
            String rentBuildArea = row.getCell(26).getStringCellValue();
            String rentSoilArea = row.getCell(27).getStringCellValue();
            int assetTypeId = (int) row.getCell(28).getNumericCellValue();


            RentOutContractCURDInfo rentOutContractCURDInfo = new RentOutContractCURDInfo(null,contractId,assetId,renterType,systemRent,counterBooth,rentUse,subletState,contractBeginDate,contractEndDate,freeBeginDate,freeEndDate,growthRate,deposit,contractStatusId,businessStatusId,remark, listed,rentBuildArea,rentSoilArea,lessor,rentInContractId,annualRent,payMethodId,rentchargeStatusId,name,contact,phone,companyName,assetTypeId);
            return rentOutAddService.insert(rentOutContractCURDInfo);

        }

        //2 读取的内容存储到数据库  list


        return Result.error("excel表格导入失败!");
    }

}
